import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Update_Demo {
    public static void main(String[] args) throws SQLException {
        Scanner scan = new Scanner(System.in);
        int age;
        String name;
        //1、创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/work_0318?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        //2、和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        //3、构成操作数据库的sql语句
        String sql = "update test set age = ? where name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        int n = preparedStatement.executeUpdate();
        System.out.println("n = " + n);

        //4、释放资源
        preparedStatement.close();
        connection.close();
    }
}
